layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form','treetable'], function(){
	  var form = layui.form;
	  var table = layui.table;
	  var $ = layui.$;



    var editable = function(d){
        if(d.parentId != '-1'){
            return 'text'; // 这里假设以 editable 字段为判断依据
        }
    };

      var Page = {
                  tableId: 'sys-cache-table',
                  url:CXL.ctxPath + '/sys/cache/infoList',
                  updateUrl:CXL.ctxPath + '/sys/cache/update',
                  toolbar:'#sys-cache-toolbar',
                  initColumn:function(){
                      return [[
                          {type : 'numbers',title : '序号',width:'3%'},
                          {field:'cacheId',title:'cacheId',hide:true,width:'1%'},
                          {field:'cacheKey',title:'cacheKey',hide:true,width:'1%'},
                          {field :'cacheName',title :'缓存名称',width:'12%'},
                          {field:'cacheValue',title:'缓存数值',width:'10%',edit: editable},
                          {field:'cacheDataType',title:'缓存数值类型',width:'8%'},
                          {field:'cacheDesc',title:'缓存说明',width:'25%'},
                          {title:'操作',width:'15%',toolbar: '#sys-cache-tool'}
                      ]];
                  },
              };


    form.on('select(select-cacheType)', function (data){
        var cacheType = data.value; // 获得被选中的值


        table.render({
            elem:'#' + Page.tableId,
            url:Page.url,
            height:'500px',
            page:false,
            autoSort:false,
            where:{cacheType:cacheType},
            cols:Page.initColumn()
        });


    });


    /**
       * 表格工具栏
       */
      table.on('tool(' + Page.tableId + ')', function(obj){
         //获得当前行数据
        var data = obj.data;

        var cacheType = data.cacheId;

        var cacheValue = data.cacheValue;

        var cacheKey = data.cacheKey;
         //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var layEvent = obj.event;

        //编辑操作
        if(layEvent === 'update'){
          Page.update(cacheType,cacheValue,cacheKey);
        }
      });


      Page.update = function (cacheType,cacheValue,cacheKey){

          parent.layer.confirm('确定要修改吗？',function(){

              //提交 Ajax进行后端修改
              $.ajax({
                  type: "post",
                  url: Page.updateUrl,
                  data: {cacheType:cacheType,cacheValue:cacheValue,cacheKey:cacheKey},
                  success: function(res) {
                      if(res.success) {
                          CXL.success(res.msg);
                      }else {
                          CXL.warn(res.msg);
                      }
                  }
              });

          });


      };

  })